﻿namespace KeRui.TemplateMaker
{
    using KeRui.BLL;
    using KeRui.Model;
    using iPortal.TemplateEngine;
    using System;
    using System.IO;
    using System.Text;

    public class Resolve
    {
        private static void GeneratePath(string path)
        {
            string[] strArray = path.Split(@"\".ToCharArray());
            string str = strArray[0];
            for (int i = 1; i < strArray.Length; i++)
            {
                str = str + @"\" + strArray[i];
                if (!Directory.Exists(str))
                {
                    Directory.CreateDirectory(str);
                }
            }
        }

        public int PublishContentPage(int[] contentIDs, bool isComplete)
        {
            return contentIDs.Length;
        }

        public static string PublishNodePage(int nodeID, int PageIndex)
        {
            TemplateDocument template = new TemplateBase().GetTemplate(PublicTypeEnum.PublicType.Node, nodeID);
            KeRui.BLL.CmsNode node = new KeRui.BLL.CmsNode();
            ResolveTag.ResolveTemplate(template);
            ResolveTag.ResolveForeach(template);
            ResolveTag.ResolveNodeListData2(template, nodeID, PageIndex, node.GetModelByCache(nodeID).ContentPageSize);
            ResolveTag.ResolveLocation(template, nodeID, PageIndex);
            ResolveTag.ResolveNodePage(template, nodeID);
            return ResolveTag.CompressHtml(template).GetRenderText();
        }

        public static string PublishNodePage(int nodeID, int PageIndex, string selectedValue)
        {
            TemplateBase base2 = new TemplateBase();
            KeRui.BLL.CmsNode node = new KeRui.BLL.CmsNode();
            TemplateDocument template = base2.GetTemplate(PublicTypeEnum.PublicType.Node, nodeID);
            ResolveTag.ResolveForeach(template);
            ResolveTag.ResolveNodeListData(template, nodeID, PageIndex, node.GetModelByCache(nodeID).ContentPageSize);
            ResolveTag.ResolveLocation(template, nodeID, PageIndex);
            ResolveTag.ResolveNodePage(template, nodeID);
            ResolveTag.ResolveTab(template, nodeID, selectedValue, string.Empty);
            return template.GetRenderText();
        }

        public static string PublishNodePage(int nodeID, int pageIndex, string selectedValue, string strWhere, string postUrl)
        {
            TemplateDocument template = new TemplateBase().GetTemplate(PublicTypeEnum.PublicType.Node, nodeID);
            ResolveTag.ResolveForeach(template);
            KeRui.BLL.CmsNode node = new KeRui.BLL.CmsNode();
            ResolveTag.ResolveNodeListData(template, nodeID, strWhere, pageIndex, node.GetModelByCache(nodeID).ContentPageSize);
            ResolveTag.ResolveLocation(template, nodeID, pageIndex);
            ResolveTag.ResolveNodePage(template, nodeID);
            ResolveTag.ResolveTab(template, nodeID, selectedValue, postUrl);
            return template.GetRenderText();
        }

        public void ResolvePage(PublicTypeEnum.PublicType publicType, int dataID)
        {
            KeRui.Model.CmsNode modelByCache;
            TemplateDocument template = new TemplateBase().GetTemplate(publicType, dataID);
            switch (publicType)
            {
                case PublicTypeEnum.PublicType.Index:
                {
                    KeRui.BLL.CmsSite site = new KeRui.BLL.CmsSite();
                    ResolveTag.ResolveForeach(template);
                    GeneratePath(site.GetModelByCache(dataID).SiteDir);
                    ResolveTag.CompressHtml(template).RenderTo(site.GetModelByCache(dataID).SiteDir + @"\Index.shtml", Encoding.GetEncoding("utf-8"));
                    return;
                }
                case PublicTypeEnum.PublicType.Node:
                    modelByCache = new KeRui.BLL.CmsNode().GetModelByCache(dataID);
                    ResolveTag.ResolveTemplate(template);
                    ResolveTag.ResolveForeach(template);
                    switch (modelByCache.NodeType)
                    {
                        case 1:
                            ResolveTag.ResolveNodeListData(template, dataID, 1, modelByCache.ContentPageSize);
                            ResolveTag.ResolveLocation(template, dataID, 1);
                            ResolveTag.ResolveTab(template, dataID, string.Empty, string.Empty);
                            break;

                        case 4:
                            ResolveTag.ResolveNodeListData(template, dataID, 1, modelByCache.ContentPageSize);
                            ResolveTag.ResolveLocation(template, dataID, 1);
                            ResolveTag.ResolveTab(template, dataID, string.Empty, string.Empty);
                            break;
                    }
                    break;

                case PublicTypeEnum.PublicType.Content:
                    return;

                default:
                    return;
            }
            template = ResolveTag.CompressHtml(ResolveTag.ResolveNodePage(template, dataID));
            GeneratePath(modelByCache.NodeDir);
            template.RenderTo(modelByCache.NodeDir + @"\Index.shtml", Encoding.GetEncoding("utf-8"));
        }
    }
}

